﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace Code.UI
{
    public class CardItem:MonoBehaviour,IPointerClickHandler,IPointerEnterHandler,IPointerExitHandler
    {
        public Action<CardItem,Card> OnSelectCard;
        [SerializeField] private TMP_Text _nameText;
        [SerializeField] private Image _cardTexture;
        [SerializeField] private TMP_Text _cardDesc;
        [SerializeField] private GameObject _selectMask;
        [SerializeField] private GameObject _enterMask;
        private UIPlayer player;
        private Card _card;
        internal void Init(Card item, UIPlayer _uiPlayer)
        {
            player = _uiPlayer;
            _card = item;
            _nameText.text = item.Name;
            _cardTexture.sprite = item.CardTexture;
            _cardDesc.text = item.Desc;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            _selectMask.SetActive(true);   
            player.UseCard(_card);
            OnSelectCard?.Invoke(this,_card);
        }

        internal void SetSelectMask(bool active)
        {
            _selectMask.SetActive(active);
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            Debug.Log("enter");
            _enterMask.SetActive(true);
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            Debug.Log("exit");
            _enterMask.SetActive(false);
        }
    }
}
